//-----------------Sonic CD Menu Window Script----------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//

// Aliases
#alias Object.Value0		:	Object.Timer
#alias Object.Value1		:	Object.Width
#alias Object.Value2		:	Object.Height
#alias Object.Value3		:	Object.XOffset
#alias Object.Value4		:	Object.YOffset
#alias Object.Value5		:	Object.EntityPos

// Object[1] is a blank object just to share values
#alias Object[1].Value4		:	Object.BackTouchPress

// States
#alias 0	:	MENUWINDOW_EXPAND
#alias 1	:	MENUWINDOW_SCALEDOWN
#alias 2	:	MENUWINDOW_BLANK
#alias 3	:	MENUWINDOW_SHRINK
#alias 4	:	MENUWINDOW_FIT_IN_BUTTON
#alias 5	:	MENUWINDOW_SOUNDTRACK_EXPAND
#alias 6	:	MENUWINDOW_SOUNDTRACK_SHRINK

// Menu Button States
#alias 7	:	MENUBUTTON_RESET

// Languages
#alias 0	:	LANG_ENGLISH
#alias 1	:	LANG_FRENCH
#alias 2	:	LANG_ITALIAN
#alias 3	:	LANG_DEUTSCH
#alias 4	:	LANG_SPANISH
#alias 5	:	LANG_JAPANESE

// Ink Effects
#alias 2	:	INK_ALPHA


sub ObjectMain
	switch Object.State
	case MENUWINDOW_EXPAND
		if Object.Timer < 14
			Object.Timer++

			// Width
			TempValue0    = 300
			TempValue0   -= Object.Width
			TempValue0  >>= 3
			Object.Width += TempValue0

			// Height
			TempValue0     = 224
			TempValue0    -= Object.Height
			TempValue0   >>= 3
			Object.Height += TempValue0

			// XPos
			TempValue0   = Screen.CenterX
			TempValue0 <<= 16
			TempValue0  -= Object.XPos
			TempValue0 >>= 2
			Object.XPos += TempValue0

			// YPos
			TempValue0   = Screen.CenterY
			TempValue0  -= 6
			TempValue0 <<= 16
			TempValue0  -= Object.YPos
			TempValue0 >>= 2
			Object.YPos += TempValue0
		else
			Object.Timer = 0
			Object.State++
		end if
		break
		
	case MENUWINDOW_SCALEDOWN
		if Object.Timer < 30
			Object.Timer++

			// Width
			TempValue0    = 256
			TempValue0   -= Object.Width
			TempValue0  >>= 2
			Object.Width += TempValue0

			// Height
			TempValue0     = 160
			TempValue0    -= Object.Height
			TempValue0   >>= 2
			Object.Height += TempValue0

			// XPos
			TempValue0   = Screen.CenterX
			TempValue0 <<= 16
			if Object.XPos > TempValue0
				Object.XPos -= 0x10000
			end if

			if Object.XPos < TempValue0
				Object.XPos += 0x10000
			end if

			// YPos
			TempValue0   = Screen.CenterY
			TempValue0  -= 8
			TempValue0 <<= 16
			if Object.YPos > TempValue0
				Object.YPos -= 0x10000
			end if

			if Object.YPos < TempValue0
				Object.YPos += 0x10000
			end if
		else
			Object.Timer = 0
			Object.State++
		end if
		break

	case MENUWINDOW_BLANK
		// Nothing
		break

	case MENUWINDOW_SHRINK
		if Object.Timer < 14
			Object.Timer++

			// Width
			TempValue0   = 132
			TempValue0  -= Object.Width
			TempValue0 >>= 3
			Object.Width += TempValue0

			// Height
			TempValue0   = 18
			TempValue0  -= Object.Height
			TempValue0 >>= 3
			Object.Height += TempValue0

			// XPos
			TempValue0   = Object.XOffset
			TempValue0  -= Object.XPos
			TempValue0 >>= 2
			Object.XPos += TempValue0

			// YPos
			TempValue0   = Object.YOffset
			TempValue0  -= Object.YPos
			TempValue0 >>= 2
			Object.YPos += TempValue0
		else
			Object.Timer = 0
			Object.State++
		end if
		break
		
	case MENUWINDOW_FIT_IN_BUTTON
		if Object.Timer < 14
			Object.Timer++

			// Width
			TempValue0   = 144
			TempValue0  -= Object.Width
			TempValue0 >>= 3
			Object.Width += TempValue0

			// Height
			TempValue0   = 21
			TempValue0   -= Object.Height
			TempValue0 >>= 3
			Object.Height += TempValue0

			// XPos
			if Object.XPos > Object.XOffset
				Object.XPos -= 0x10000
			end if

			if Object.XPos < Object.XOffset
				Object.XPos += 0x10000
			end if

			// YPos
			if Object.YPos > Object.YOffset
				Object.YPos -= 0x10000
			end if

			if Object.YPos < Object.YOffset
				Object.YPos += 0x10000
			end if
		else
			Object.Timer++
			if Object.Timer == 16
				Object.Timer = 0
				Object.Type = TypeName[Blank Object]
			end if

			ArrayPos0 = Object.EntityPos
			Object[ArrayPos0].Type = TypeName[Menu Button]
			Object[ArrayPos0].State = MENUBUTTON_RESET
		end if
		break

	case MENUWINDOW_SOUNDTRACK_EXPAND
		if Object.Timer < 14
			Object.Timer++

			// Width
			TempValue0    = 300
			TempValue0   -= Object.Width
			TempValue0  >>= 3
			Object.Width += TempValue0

			// Height
			TempValue0     = 192
			TempValue0    -= Object.Height
			TempValue0   >>= 3
			Object.Height += TempValue0

			// XPos
			TempValue0   = Screen.CenterX
			TempValue0 <<= 16
			TempValue0  -= Object.XPos
			TempValue0 >>= 2
			Object.XPos += TempValue0

			// YPos
			TempValue0   = Screen.CenterY
			TempValue0  -= 6
			TempValue0 <<= 16
			TempValue0  -= Object.YPos
			TempValue0 >>= 2
			Object.YPos += TempValue0
		else
			Object.Timer = 0
			Object.State++
		end if
		break

	case MENUWINDOW_SOUNDTRACK_SHRINK
		if Object.Timer < 30
			Object.Timer++

			// Width
			TempValue0    = 256
			TempValue0   -= Object.Width
			TempValue0  >>= 2
			Object.Width += TempValue0

			// Height
			TempValue0     = 128
			TempValue0    -= Object.Height
			TempValue0   >>= 2
			Object.Height += TempValue0

			// XPos
			TempValue0   = Screen.CenterX
			TempValue0 <<= 16

			if Object.XPos > TempValue0
				Object.XPos -= 0x10000
			end if

			if Object.XPos < TempValue0
				Object.XPos += 0x10000
			end if

			// YPos
			TempValue0   = Screen.CenterY
			TempValue0  -= 8
			TempValue0 <<= 16

			if Object.YPos > TempValue0
				Object.YPos -= 0x10000
			end if

			if Object.YPos < TempValue0
				Object.YPos += 0x10000
			end if
		else
			Object.Timer = 0
			Object.State = MENUWINDOW_BLANK
		end if
		break

	end switch
end sub


sub ObjectDraw
	// Upper Left Border
	TempValue0  = Object.Width
	TempValue0 *= -0x8000
	TempValue0 += Object.XPos
	TempValue1  = Object.Height
	TempValue1 *= -0x8000
	TempValue1 += Object.YPos
	DrawSpriteXY(0, TempValue0, TempValue1)

	// Upper Right Border
	TempValue0   = Object.Width
	TempValue0 <<= 15
	TempValue0  += Object.XPos
	DrawSpriteXY(1, TempValue0, TempValue1)

	// Bottom Left Border
	TempValue0   = Object.Width
	TempValue0  *= -0x8000
	TempValue0  += Object.XPos
	TempValue1   = Object.Height
	TempValue1 <<= 15
	TempValue1  += Object.YPos
	DrawSpriteXY(2, TempValue0, TempValue1)

	// Bottom Right Border
	TempValue0   = Object.Width
	TempValue0 <<= 15
	TempValue0  += Object.XPos
	DrawSpriteXY(3, TempValue0, TempValue1)

	// Menu Filling
	TempValue0   = Object.Width
	TempValue0  *= -0x8000
	TempValue0  += Object.XPos
	TempValue0 >>= 16
	TempValue1   = Object.Height
	TempValue1  *= -0x8000
	TempValue1  += Object.YPos
	TempValue1  += 0xA0000
	TempValue1 >>= 16
	TempValue2   = Object.Width
	TempValue2  -= 2
	TempValue3   = Object.Height
	TempValue3  -= 21
	DrawRect(TempValue0, TempValue1, TempValue2, TempValue3, 96, 96, 128, 255)

	// Menu Shadow
	TempValue0   = Object.Width
	TempValue0 <<= 15
	TempValue0  += Object.XPos
	TempValue0  -= 0x20000
	TempValue0 >>= 16
	TempValue2   = 2
	DrawRect(TempValue0, TempValue1, TempValue2, TempValue3, 0, 0, 0, 255)

	Object.InkEffect = INK_ALPHA
	switch Object.State
	case MENUWINDOW_EXPAND
	case MENUWINDOW_SCALEDOWN
	case MENUWINDOW_SOUNDTRACK_EXPAND
	case MENUWINDOW_SOUNDTRACK_SHRINK
		if Object.Alpha < 248
			Object.Alpha += 5
		end if
		break

	case MENUWINDOW_BLANK
		Object.Alpha = 255
		break

	case MENUWINDOW_SHRINK
	case MENUWINDOW_FIT_IN_BUTTON
		if Object.Alpha > 0
			Object.Alpha -= 8
		end if
		break
		
	end switch

	TempValue0  = Screen.XSize
	TempValue0 -= 48
	if Object.BackTouchPress == true
		DrawSpriteScreenFX(5, FX_INK, TempValue0, 225)
	else
		DrawSpriteScreenFX(4, FX_INK, TempValue0, 225)
	end if
end sub


sub ObjectStartup
	switch Engine.Language
	case LANG_ENGLISH
		LoadSpriteSheet("Menu/MenuGfx2_EN.gif")
		break

	case LANG_FRENCH
		LoadSpriteSheet("Menu/MenuGfx2_FR.gif")
		break

	case LANG_ITALIAN
		LoadSpriteSheet("Menu/MenuGfx2_IT.gif")
		break

	case LANG_DEUTSCH
		LoadSpriteSheet("Menu/MenuGfx2_DE.gif")
		break

	case LANG_SPANISH
		LoadSpriteSheet("Menu/MenuGfx2_ES.gif")
		break

	case LANG_JAPANESE
		LoadSpriteSheet("Menu/MenuGfx2_JP.gif")
		break

	end switch
	
	// (#0 - #3) - Borders for the Menu Window, Upper Left - Right - Bottom Left - Bottom Right
	SpriteFrame(0, 0, 140, 10, 259, 246)
	SpriteFrame(-140, 0, 140, 10, 263, 246)
	SpriteFrame(0, -11, 140, 11, 259, 256)
	SpriteFrame(-140, -11, 140, 11, 263, 256)

	SpriteFrame(-32, -24, 64, 24, 404, 146)		// #4 - Back Button
	SpriteFrame(-32, -24, 64, 24, 404, 171)		// #5 - Back Button Highlight
end sub


// ========================
// Editor Subs
// ========================

sub RSDKDraw
	DrawSprite(0)
end sub


sub RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)		// #0 - "Script" Icon

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end sub
